<?php

/**
 * Table/OperationTable.php
 */

namespace RHX\Model\Table;

use Zend\Db\Adapter\Adapter;
use Zend\Db\TableGateway\AbstractTableGateway;
use Zend\Db\ResultSet\ResultSet;
use RHX\Model\Object\PlanningSemaine;
use RHX\Model\Object\XCalculator;
use RHX\Model\Object\XRules;
use RHX\Helper\RHDateTime;

class OperationTable extends PlanningSemaineTable {
    /* protected $table = 'rh_planning_semaine'; */

    public function __construct(Adapter $adapter) {
	parent::__construct($adapter);
    }

    /**
     * Récupération de la main d'oeuvre à une date donnée
     *
     * @param int $siteId
     * @param string $date
     * @return real
     */
    public function getMO($siteId, $date = null) {
	$mo		 = 0.0;
	$calculator	 = new XCalculator($this->getAdapter());
	$tableEmploye	 = new EmployeTable($this->getAdapter());
	$employeList	 = $tableEmploye->getEmployesBySite($siteId);
	$employeId	 = array();
	foreach ($employeList as $employe) {
	    $employeId[] = $employe->matricule;
	}
	$plannings = $this->select(function(\Zend\Db\Sql\Select $select) use($date, $employeId) {
		    $where = new \Zend\Db\Sql\Where();
		    $where->in('matricule', $employeId);
		    $where->literal("? BETWEEN lundi AND dimanche");
		    $select->where($where);
		});
	// parcours du planning
	$days	 = array(
	    'lundi', 'mardi', 'mercredi', 'jeudi', 'vendredi', 'samedi', 'dimanche',
	);
	$dateRef = new RHDateTime($date);
	foreach ($plannings as $planning) {
	    foreach ($days as $day) {
		$jour = new RHDateTime($planning->$day);
		if ($dateRef->isEqual($jour)) {
		    // calcul
		    $data = $calculator->calculateOneDay($planning, $planning->getTemplate(), $day);
		    $mo += $data->montant;
		}
	    }
	}
	return $mo;
    }

}

?>
